# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from datetime import datetime
from odoo.exceptions import ValidationError
from odoo.tools import float_compare
from odoo.exceptions import except_orm, Warning, RedirectWarning
import odoo.addons.decimal_precision as dp
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo import api, fields, models


class mrp_product_replace(models.Model):
    _name = 'mrp.product.replace'
    _description = 'replace'
    _order = 'name'
    _inherit = ['mail.thread']

    name = fields.Many2one('product.product', u'产品')
    product_qty = fields.Float(u'原用量')
    note = fields.Char(u'备注')
    active = fields.Boolean(u'有效', default=True)
    date = fields.Datetime(u'日期', select=True, default=lambda self: fields.Datetime.now())
    user_id = fields.Many2one('res.users', u'责任人', default=lambda self: self.env.user)
    replace_product_id = fields.Many2one('product.product', u'替代品')
    mrp_bom_line_id = fields.Many2one('mrp.bom.line', u'bom明细行', ondelete='cascade')
    replace_product_qty = fields.Float(u'替代用量')
    attrition_rate = fields.Float(u'耗损率', default=0.0)
    attrition_fixed = fields.Float(u'固定耗损', default=0.0)
    product_available = fields.Float(related='replace_product_id.qty_available', string=u'在手')
    product_incoming = fields.Float(related='replace_product_id.incoming_qty', string=u'在途')
    replace_type = fields.Selection([
        ('user_decision', u'自定'),
        ('no', u'禁止'),
        ('all', u'完全'),
        ('available', u'可用替代')
    ], u'替代方式', default="user_decision")

    @api.onchange('mrp_bom_line_id')
    def _onchange_mrp_bom_line_id(self):
        if self.mrp_bom_line_id:
            self.name = self.mrp_bom_line_id.product_id.id
            self.product_qty = self.mrp_bom_line_id.product_qty
            self.replace_product_qty = self.mrp_bom_line_id.product_qty
            self.attrition_rate = self.mrp_bom_line_id.attrition_rate
            self.attrition_fixed = self.mrp_bom_line_id.attrition_fixed


            # 重写name_get方法，便于其它地方引用

    @api.multi
    @api.depends('name', 'note', 'replace_product_id', 'replace_type')
    def name_get(self):
        result = []
        for replace in self:
            replace_name = replace.replace_product_id.display_name + \
                           u"[在手：" + str(replace.product_available) + u" 在途：" + str(replace.product_incoming) + "]"
            if replace.note:
                replace_name += u"[" + replace.note + u"]"
            if replace.replace_type:
                # 获取selection字段的选项值
                type_list = replace.fields_get('replace_type')['replace_type']['selection']
                for re_type in type_list:
                    if re_type[0] == replace.replace_type:
                        type_name = re_type[1]
                replace_name += type_name
            replace_name = u"【" + replace_name + u"】"
            result.append((replace.id, replace_name))
        return result
